class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        // if(root == NULL) return NULL;
        // TreeNode* left = root->left;
        // root->left = root->right;
        // root->right = left;
        // invertTree(root->left);
        // invertTree(root->right);
        // return root;
        
        if(root == NULL) return root;
        stack<TreeNode*> st;
        st.push(root);
        while(!st.empty()){
            TreeNode* cur = st.top();
            st.pop();
            swap(cur->left,cur->right);
            if(cur->left) st.push(cur->left);
            if(cur->right) st.push(cur->right);
        }
        return root;
    }
};
